// orderDetailState.js
var app = getApp()
let upload = ''
import { formatOrder, formatMoney, formatRaty, formatOrderNo, orderEvents, formatTime } from "../../utils/util.js";
import { previewImage } from '../../utils/camera.js';
import { showTi, showToast, showLoading } from "../../utils/modal.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_type: null,
    order_Id: null,
    orderDetailState: {},
    monyStars: null,
    auditNodeList: [],
    // installData: null,
    userId: null,
    flowsList:[],
    examineList:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this      
    self.setData({
      order_Id: options.order_Id,
      userId: wx.getStorageSync('wechat_user'),
    }) 
    self.orderDetailState()
    upload = getCurrentPages()[1]
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    upload.orderDetailList()
    upload.orderStateNum()
    wx.removeStorageSync('facilitator')
  },
  //获取详情状态列表
  orderDetailState: function () {
    let self = this
    // let assignObj = wx.getStorageSync('facilitator')
    // self.setData({
    //   installData: assignObj
    // }) 
    // console.log(this.data.installData)  
    app.query("web/orders/" + self.data.order_Id).then(res => {
      if (res.code === 200) {
        console.log(res.order)
        let formatOrders = formatOrder(res.order)
        formatOrders.customer_service_info.raty = formatOrderNo(formatOrders)
        formatOrders.order_events = orderEvents(formatOrders.order_events)
        console.log(formatOrders)
        self.setData({
          orderDetailState: formatOrders,
        })
        self.stars(self.data.orderDetailState.customer_service_info.raty)
        self.stars(self.data.orderDetailState.service_info.raty)
        self.auditNode()
      }
    })
  },
  //星星判断
  stars: function (startNum) {
    let self = this
    let starsList = []
    let imgObj = { url: "../img/me/order_star.png" }
    if (startNum != null) {
      startNum = (parseInt(startNum / 1))
      switch (startNum) {
        case 1: starsList.push(imgObj); break;
        case 2: starsList.push(imgObj, imgObj); break;
        case 3: starsList.push(imgObj, imgObj, imgObj); break;
        case 4: starsList.push(imgObj, imgObj, imgObj, imgObj); break;
        case 5: starsList.push(imgObj, imgObj, imgObj, imgObj, imgObj); break;
      }
      this.setData({
        monyStars: starsList
      })
      console.log(starsList)
    }
  },
  //各种状态下的按钮点击
  saveCard: function (e) {
    console.log(e)
    let butType = e.currentTarget.dataset.buttype
    let order = e.currentTarget.dataset.order
    let orderDetailStateObj = JSON.stringify(this.data.orderDetailState);
    switch (butType) {
      case 'price': this.next('../upLoad/upLoad?orderId=' + order.id + '&type=' + "price"); break;
      case 'installgo': this.next('../assignInstall/assignInstall?orderId=' + this.data.orderDetailState.id); break;
      case 'measureNo': this.next('../upLoad/upLoad?orderId=' + order.id + '&type=' + "measure_pictures"); break;
      case 'assign': this.assign(order); break;
      case 'installTime': this.next("../calendar/calendar?orderDetailState=" + orderDetailStateObj); break;
      case 'installPhoto': this.next('../upLoad/upLoad?orderId=' + order.id + '&type=' + "completed_pictures"); break;
    }
  },
  next: function (url) {
    wx.navigateTo({
      url: url
    })
  },
  //获取审核节点
  auditNode: function () {
    let self = this
    var i
    var j
    var t
    var p
    var resultList=[]
    var resultObj={}
    var flowsListData=[]
    var examineListData=[]
    app.query("web/orders/" + this.data.order_Id + "/fetch_audit_users").then(res => {
      if (res.code === 200) {
        console.log(res)
        self.setData({
          auditNodeList: res
        })
        for (t = self.data.orderDetailState.flows.length - 1; t >= 0; t--) {
          flowsListData.push(self.data.orderDetailState.flows[t])
          self.setData({
            flowsList: flowsListData
          })
        }
        for (var z in self.data.auditNodeList.result) {
          resultList.push(self.data.auditNodeList.result[z])
        }
        for (i = 0, j = 0; i < self.data.flowsList.length, j < resultList.length; i++ , j++) {
          resultList[j].map(function (v) {
            resultObj = v
          })
          if (self.data.flowsList[i].user_id == resultObj.id) {
            examineListData.push(resultObj)
            examineListData[j].confirm = self.data.flowsList[i].confirm
            examineListData[j].updated_at = formatTime(flowsListData[j].updated_at)
            self.setData({
              examineList: examineListData
            })  
          } 
        }
        console.log(self.data.examineList)
      }
    })  
  },
  //确认指派
  // assign: function () {
  //   let self = this
  //   let assignObj = wx.getStorageSync('facilitator')
  //   self.setData({
  //     installData: assignObj
  //   })  
  //   console.log(self.data.installData)
  //   let assignData = {
  //     id: self.data.installData.id,
  //     name: self.data.installData.real_name,
  //     tel: self.data.installData.tel,
  //     facilitator_id: self.data.installData.id,
  //   }
  //   if (self.data.installData){
  //     app.query("web/orders/" + self.data.orderDetailState.id, assignData, 'put').then(res => {
  //       if (res.code === 200) {
  //         showToast("指派成功")
  //         self.orderDetailState()
  //         console.log(res)
  //       }
  //     })
  //   }else{
  //     showTi('请指派安装师傅');
  //   }
  // },
  //图片点击
  previewImage: function (e) {
    console.log(e)
    let dataImg = []
    if (e.currentTarget.dataset.type == 'price') {
      dataImg.push(e.target.dataset.obj.image.url)

    } else {
      for (var i in e.target.dataset.obj) {
        dataImg.push(e.target.dataset.obj[i].image.url)
      }
      console.log(dataImg)     
    }
    wx.previewImage({
      current: e.target.id, // 当前显示图片的http链接  
      urls: dataImg // 需要预览的图片http链接列表  
    })
  },
  //拨打电话
  callPhone: function (e) {
    console.log(e)
    let tel = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: tel //仅为示例，并非真实的电话号码
    })
  },
})